

# 要求data有序
def binary_search(data, target):
    left = 0
    right = len(data) - 1

    while left <= right:  # 保证data至少有一个元素
        mid = (left + right) // 2
        if data[mid] == target:
            return mid
        elif data[mid] > target:
            right = mid - 1
        else:
            left = mid + 1

    return -1


data = list(range(100))
print(binary_search(data, 83))
print(binary_search(data, 199))

